package com.example.lagody.com;

import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

public class ActivityMain extends Activity implements AdListener {
	WebView web;
	ProgressBar prBar;
	private InterstitialAd interstitial;
	AdRequest adRequest;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		web = (WebView) findViewById(R.id.wv);
		prBar = (ProgressBar) findViewById(R.id.progressBar);
		interstitial = new InterstitialAd(this,
				"ca-app-pub-5749889588405632/1398583100");
		adRequest = new AdRequest();		
		interstitial.setAdListener(this);
		AdView adView = (AdView) this.findViewById(R.id.adView);
		adView.loadAd(new AdRequest());
		if (checkIntenet()) {
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://www.lagody.com/");
			Handler han=new Handler();
			han.postDelayed(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					interstitial.loadAd(adRequest);
					
				}
			}, 1000);
			
		} else {
			AlertDialog alert = new AlertDialog.Builder(ActivityMain.this)
					.create();
			alert.setTitle("Thông Báo");
			alert.setMessage("Bạn chưa kết nối mạng!");
			alert.setButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {

				}
			});
			alert.show();
		}
		
		
	}
	public final boolean checkIntenet() {
		ConnectivityManager connectmanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectmanager.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
				|| connectmanager.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING
				|| connectmanager.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING
				|| connectmanager.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) {
			return true;
		} else if (connectmanager.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
				|| connectmanager.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
			return false;
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.itemHome:
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://www.lagody.com/");
			return true;
		case R.id.itemVideo:
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://video.lagody.com/");
			return true;
		case R.id.itemPhapluat:
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://phapluat.lagody.com/");
			return true;
		case R.id.itemXahoi:
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://xahoi.lagody.com/");
			return true;
		case R.id.itemGiamcan:
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://giamcan.lagody.com/");
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	public class clientAsyn extends WebViewClient {
		@Override
		public void onPageStarted(WebView view1, String url1, Bitmap favicon1) {
			super.onPageStarted(view1, url1, favicon1);
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view1, String url1) {
			view1.loadUrl(url1);
			return true;

		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			view.loadUrl("javascript:document.getElementById('wrapper').remove();document.getElementById('footer').remove();");
			prBar.setVisibility(View.GONE);
		}

	}

	@Override
	public boolean onKeyDown(int keyCode1, KeyEvent event1) {
		if ((keyCode1 == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
			web.goBack();
			return true;
		}
		return super.onKeyDown(keyCode1, event1);
	}

	@Override
	public void onDismissScreen(Ad arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onLeaveApplication(Ad arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPresentScreen(Ad arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onReceiveAd(Ad arg0) {
		// TODO Auto-generated method stub
		Log.d("OK", "Received ad");
		if (arg0 == interstitial) {
			interstitial.show();
		}
	}
}